// 经常和工厂模式搭配使用，这里其实类似工厂方法模式

class FlyweightFactory {
    // @ts-ignore
    map: Map<string, Flyweight> = new Map<string, Flyweight>();
    getFlyweight(name: string) {
        if (!this.map.has(name)) {
            this.map.set(name, new Flyweight(name));
        }
        return this.map.get(name);
    }
}

class Flyweight {
    state: string = '';
    constructor(state: string) {
        console.log('constructor ... state is ', state);
        this.state = state;
    }
    operate(): void {
        console.log('this state is ', this.state);
    }
}

const factory = new FlyweightFactory();
factory.getFlyweight('yang').operate();
factory.getFlyweight('yang').operate();